#define DEFERRED_DIR_LIGHT_HLSL
#pragma pack_matrix(row_major)
#include "Defs/LightSupport.hlsli"
#include "Defs/ConstantBuffers.hlsli"
#include "Defs/Textureing.hlsli"

float4 main(float4 position : SV_POSITION, float2 uv : TEXCOORD) : SV_TARGET0
{
	float3 specular = specularMap.Sample(linearClampSampler, uv);
	float3 diffuse = diffuseMap.Sample(linearClampSampler, uv);
	float ambient = normalMap.Sample(linearClampSampler, uv).a;
	float3 normal = normalMap.Sample(linearClampSampler, uv).rgb;
		float outputAlpha = normalMap.Sample(linearClampSampler, uv).a;
		normal = (normal - .5f) * 2.f;
		float4 scrPos = depthMap.Sample(linearClampSampler, uv);
			scrPos = (scrPos + .5f) * 2.f;
		float4 posWorld = scrPos;

		float nDotL = saturate(dot(-gDirectionalLight.direction.xyz, normal));
	float3 directionToCamera = normalize(gCameraPosition.xyz - posWorld);
		float3 reflectionVector = reflect(normal, directionToCamera);
		float specular_nDotL = saturate(dot(reflectionVector, directionToCamera)) * nDotL;

	float4 finalDiffuse = float4(diffuse, 1.f) * gDirectionalLight.diffuseColor * nDotL;
		float4 finalSpecular = float4(specular, 1.f) * gDirectionalLight.specularColor * specular_nDotL;
		return float4((finalDiffuse + finalSpecular).rgb, outputAlpha);

#if 0
	normal = (normal - .5f) * 2.f;

	float4 posWorld = CalculateWorldSpacePosition(position.xy, depth, gInvViewProj);
		float4 lightSpacePos;
	float4 finalAmbient, finalDiffuse, finalSpecular;

	float3 directionToCamera = normalize(gCameraPosition - posWorld);
		float3 reflectionVector = reflect(normal, directionToCamera);
		float lightMapDepth;
	float shadow, offset;
	float nDotL = saturate(dot(normal, gDirectionalLight.direction));
	float specular_nDotL = saturate(dot(reflectionVector, directionToCamera));
	float specMod = gDirectionalLight.specularIntensity * pow(specular_nDotL, gDirectionalLight.specularPower);
	
	//float3 lightSpacePosition = mul(position, gDirectionalLight.viewProjection[0]).xyz;
	//	lightSpacePosition += (float3)1.f;
	//lightSpacePosition /= 2.f;
	//lightSpacePosition.y = 1.f - lightSpacePosition.y;

	//float shadowFactor = CalculateShadowPCF(directional0ShadowMap[0], lightSpacePosition.xy, lightSpacePosition.z);

	finalAmbient = float4(gDirectionalLight.ambientColor * float4(diffuse, 1.f) * ambient, 1.f);
	finalDiffuse = float4(diffuse, 1.f) * nDotL * float4(gDirectionalLight.diffuseColor, 1.f);
	finalSpecular = float4(specular, 1.f) * specMod * (specular_nDotL)* float4(gDirectionalLight.specularColor, 1.f);
	return finalDiffuse + finalAmbient + finalSpecular;//*shadowFactor
#endif
}
